
layui.define(['form', 'layedit', 'laydate', 'transfer', 'formSelects'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let formSelects = layui.formSelects;
    let laydate = layui.laydate;

    let effectiveDateObj = {
        elem: '#effectiveDate',
        type: 'date',
        btns: ['clear', 'confirm'],
        min: 1
    };

    laydate.render(effectiveDateObj);

    zmnLayUtil.layFormVerify(form);

    var ratioTypeData = new Array();
    var channelDiscount = new Object();
    channelDiscount['name'] = "渠道折扣系数";
    channelDiscount['value'] = 1;
    ratioTypeData.push(channelDiscount);
    var channelCity = new Object();
    channelCity['name'] = "渠道城市系数";
    channelCity['value'] = 2;
    ratioTypeData.push(channelCity);
    var channelBrand = new Object();
    channelBrand['name'] = "渠道品牌系数";
    channelBrand['value'] = 3;
    ratioTypeData.push(channelBrand);

    // 初始化产品二级分类
    formSelects.btns('ratioType', ['select', 'remove', 'reverse']);
    formSelects.data('ratioType', 'local', {
        arr: ratioTypeData
    });

    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        let ratioType = formSelects.value('ratioType', 'val');
        if (ratioType == undefined || ratioType == null || ratioType.length == 0) {
            layer.msg("系数类型不能为空");
            return false;
        }

        let data = new Object();
        data['sourceChannelId'] = obj.field.channelId;
        data['targetChannelId'] = json.channelId;
        data['bizType'] = json.bizType;
        data['priceType'] = json.priceType;
        let copyRatioType = formSelects.value('ratioType', 'val');
        data['copyRatioType'] = copyRatioType.join(',');

        if (obj.field.effectiveDate == null || obj.field.effectiveDate == '' || obj.field.effectiveDate == undefined) {
            var nowDate = new Date();
            let effectiveDateStr = new Date(nowDate.getTime() + 24*60*60*1000).format("yyyy-MM-dd");
            data['effectiveDate'] = effectiveDateStr;
        } else {
            data['effectiveDate'] = obj.field.effectiveDate;
        }

        $.ajax({
            type: "POST",
            url: "/channel/copyChannelRatio",
            dataType: "json",
            data: data,
            success: function (json) {
                if (json.status === 200) {
                    // layer.msg(json.message);
                    zmnLayUtil.closeThisIframeMsg(parent, json.message, "zmn-table");
                }else {
                    parent.layer.msg(json.message,{"icon":2});
                }
            }
        });
        return false
    });

    Date.prototype.format = function(fmt) {
        var o = {
            "M+" : this.getMonth()+1,                 //月份
            "d+" : this.getDate(),                    //日
            "h+" : this.getHours(),                   //小时
            "m+" : this.getMinutes(),                 //分
            "s+" : this.getSeconds(),                 //秒
            "q+" : Math.floor((this.getMonth()+3)/3), //季度
            "S"  : this.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt)) {
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        }
        for(var k in o) {
            if(new RegExp("("+ k +")").test(fmt)){
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
            }
        }
        return fmt;
    }

    $("#cancleBtn").click(function () {
        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });
    //对外暴露的接口
    exports('channel_ratio_copy', {});
});